<template>

	<!-- 使用 page-meta 组件配合阻止滚动穿透 ,page-meta一定作为页面的第一个节点-->
	<page-meta :page-style="'overflow:'+(show?'hidden':'visible')"></page-meta>

	<uni-popup ref="sharePopup" type="bottom" @change="changePop" :safeArea="false">
		<my-share-pop :fridge="fridge" @close="closeSharePop"></my-share-pop>
	</uni-popup>

	<view class="container">
		<view class="fridge-box">
			<view class="left">
				<image :src="fridge.backgroundUrl" style="height: 150rpx; width: 120rpx;" mode="aspectFit"></image>
				<view class="desc">
					<text class="name-text">{{fridge.name}}</text>
					<view class="brand-model-text">
						<text class="brand-text">{{fridge.brand}}</text>
						<text class="model-text">{{fridge.model}}</text>
					</view>
				</view>
			</view>
		</view>

		<view class="member-list">
			<uni-list>
				<uni-list-item clickable="true" v-for="(item ,index) in member_list" :key="index">
					<template v-slot:header>
						<view class="slot-box">
							<image class="slot-image" :src="item.avatar" mode="widthFix"></image>
						</view>
					</template>
					<template v-slot:body>
						<view class="body">
							<text class="name-text">{{item.name}}</text>
							<text class="root-text" v-if="index==0">管理员</text>
						</view>
					</template>
					<template v-slot:footer>
						<view class="footer" v-if="index!=0&&token==member_list[0].uid"
							@tap.stop="deleteMember(item.uid)">
							<uni-icons type="trash" size="30" color="#959ea8"></uni-icons>
						</view>
					</template>
				</uni-list-item>
			</uni-list>
		</view>

		<view class="invite-row" hover-class="hover-invite-row" @click="shareToggle">
			<uni-icons type="plus" size="32"></uni-icons>
			<text>邀请成员</text>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				fridge: {},
				show: false,
				member_list: []
			};
		},

		computed: {
			...mapState('user', ['token'])
		},

		onLoad(options) {
			this.fridge = JSON.parse(options.fridge)
		},

		onShow() {
			this.load()
		},

		onPullDownRefresh() {
			this.load()
			uni.stopPullDownRefresh()
		},

		onShareAppMessage() {
			this.closeSharePop()
			return {
				title: '你的好友邀请你一起来管理冰箱',
				imageUrl: this.fridge.backgroundUrl,
				path: '/pages/home/home?fid=' + this.fridge.fid
			}
		},

		methods: {

			load() {
				this.getMembers()
			},

			// 请求
			async getMembers() {
				this.member_list = await uni.$api.fridge.getMembersByFid(this.fridge.fid)
			},
			deleteMember(uid) {
				uni.showModal({
					title: 'WARNING!',
					content: '是否将该用户移出冰箱',
					success: async (res) => {
						await uni.$api.fridge.remove(uid, this.fridge.fid).then((res) => {
							uni.$showMsg(res)
							this.load()
						}).catch((reason) => {
							uni.$showMsg(reason)
						})
					}
				})
			},

			// 跳转

			// 事件处理
			shareToggle() {
				this.$refs.sharePopup.open()
			},
			changePop(e) {
				this.show = e.show
			},
			closeSharePop() {
				this.$refs.sharePopup.close()
			}

		}
	}
</script>

<style lang="scss">
	.container {
		padding-top: 30rpx;
		padding-left: 25rpx;
		padding-right: 25rpx;

		.fridge-box {
			display: flex;
			background-color: #fff;
			align-items: center;
			justify-content: space-between;

			.left {
				display: flex;
				padding: 15rpx;
				border-radius: 20rpx;
				align-items: center;
				justify-content: space-between;

				.desc {
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					height: 150rpx;
					margin-left: 15rpx;

					.name-text {
						font-size: 40rpx;
					}

					.brand-model-text {
						display: flex;
						flex-direction: column;
						color: #abb2bb;
						font-size: 20rpx;

						.model-text {
							white-space: nowrap;
						}
					}
				}
			}
		}

		.member-list {
			margin-top: 30rpx;
			overflow: hidden;
			border-radius: 20rpx 20rpx 0 0;

			.uni-list {
				.body {
					display: flex;
					flex-direction: column;
					justify-content: space-around;
					flex: 1;
					margin-left: 15rpx;

					.name-text {
						font-size: 35rpx;
					}

					.root-text {
						font-size: 20rpx;
						color: #959ea8;
					}
				}

				.footer {
					display: flex;
					align-items: center;
				}

				.slot-image {
					/* #ifndef APP-NVUE */
					display: block;
					/* #endif */
					margin-right: 10rpx;
					width: 80rpx;
					height: 80rpx;
					border-radius: 50px;
				}
			}
		}

		.invite-row {
			background-color: #fff;
			display: flex;
			font-size: 35rpx;
			align-items: center;
			justify-content: center;
			padding: 20rpx;
			border-radius: 0 0 20rpx 20rpx;

			>text {
				margin-left: 10rpx;
			}
		}

		.hover-invite-row {
			background-color: #f1f1f1
		}
	}

	.pop-content {
		display: flex;
		flex-direction: column;
		background-color: #fff;
		border-radius: 20rpx 20rpx 0 0;
		align-items: center;
		padding: 20rpx;

		.title-row {
			font-size: 22px;
			padding-bottom: 20rpx;
		}

		.list-view {
			display: flex;
			justify-content: space-evenly;
			width: 80%;

			>button {
				width: 20%;
				background-color: #fff;
				overflow: unset;
				box-sizing: unset;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				font-size: 16px;
			}

			>button::after {
				border: none;
			}
		}
	}
</style>